﻿// <copyright file="Window1.xaml.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008-2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-02-05</date>
// <summary>OpenWPFChart library. GridTest Test app.</summary>
// <revision>$Id$</revision>

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using OpenWPFChart.Parts;

namespace GridTest
{
	/// <summary>
	/// GridTest Test app main Window.
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();

			// Bind to View.Zoom Command
			InputBindings.Add(new KeyBinding(NavigationCommands.Zoom, Key.Add // + at Numeric Pad
				, ModifierKeys.Control) { CommandParameter = 1.2 });
			InputBindings.Add(new KeyBinding(NavigationCommands.Zoom, Key.Subtract // - at Numeric Pad
				, ModifierKeys.Control) { CommandParameter = -1.2 });
			CommandBindings.Add(new CommandBinding(NavigationCommands.Zoom, ZoomExecutedCommandHandler));
		}

		#region Dependency properties
		#region HorizontalGrid
		#region HorizontalGridVisibility
		/// <summary>
		/// Identifies the <see cref="HorizontalGridVisibility"/> dependency property.
		/// </summary>
		public static readonly DependencyProperty HorizontalGridVisibilityProperty
			= DependencyProperty.Register("HorizontalGridVisibility", typeof(GridVisibility), typeof(Window1)
				, new FrameworkPropertyMetadata(GridVisibility.AllTicks, FrameworkPropertyMetadataOptions.AffectsRender)
				, Visibility_Validate);
		/// <summary>
		/// Gets or sets the HorizontalGridVisibility property.
		/// </summary>
		/// <value><see cref="GridVisibility"/> value</value>
		public GridVisibility HorizontalGridVisibility
		{
			get { return (GridVisibility)GetValue(HorizontalGridVisibilityProperty); }
			set { SetValue(HorizontalGridVisibilityProperty, value); }
		}
		#endregion HorizontalGridVisibility

		#region HorizontalGridPen
		/// <summary>
		/// Identifies the <see cref="HorizontalGridPen"/> dependency property.
		/// </summary>
		public static readonly DependencyProperty HorizontalGridPenProperty
			= DependencyProperty.Register("HorizontalGridPen", typeof(Pen), typeof(Window1)
				, new FrameworkPropertyMetadata(
					new Pen(Brushes.Gray, 0.5) { DashStyle = new DashStyle(new double[] { 2, 4 }, 0) }
					, FrameworkPropertyMetadataOptions.AffectsRender));
		/// <summary>
		/// Gets or sets the HorizontalGridPen property.
		/// </summary>
		/// <value>The pen.</value>
		public Pen HorizontalGridPen
		{
			get { return (Pen)GetValue(HorizontalGridPenProperty); }
			set { SetValue(HorizontalGridPenProperty, value); }
		}
		#endregion HorizontalGridPen
		#endregion HorizontalGrid

		#region VerticalGrid
		#region VerticalGridVisibility
		/// <summary>
		/// Identifies the <see cref="VerticalGridVisibility"/> dependency property.
		/// </summary>
		public static readonly DependencyProperty VerticalGridVisibilityProperty
			= DependencyProperty.Register("VerticalGridVisibility", typeof(GridVisibility), typeof(Window1)
				, new FrameworkPropertyMetadata(GridVisibility.AllTicks, FrameworkPropertyMetadataOptions.AffectsRender)
				, Visibility_Validate);
		/// <summary>
		/// Gets or sets the VerticalGridVisibility property.
		/// </summary>
		/// <value><see cref="GridVisibility"/> value</value>
		public GridVisibility VerticalGridVisibility
		{
			get { return (GridVisibility)GetValue(VerticalGridVisibilityProperty); }
			set { SetValue(VerticalGridVisibilityProperty, value); }
		}
		#endregion VerticalGridVisibility

		#region VerticalGridPen
		/// <summary>
		/// Identifies the <see cref="VerticalGridPen"/> dependency property.
		/// </summary>
		public static readonly DependencyProperty VerticalGridPenProperty
			= DependencyProperty.Register("VerticalGridPen", typeof(Pen), typeof(Window1)
				, new FrameworkPropertyMetadata(
					new Pen(Brushes.Gray, 0.5) { DashStyle = new DashStyle(new double[] { 2, 4 }, 0) }
					, FrameworkPropertyMetadataOptions.AffectsRender));
		/// <summary>
		/// Gets or sets the VerticalGridPen property.
		/// </summary>
		/// <value>The pen.</value>
		public Pen VerticalGridPen
		{
			get { return (Pen)GetValue(VerticalGridPenProperty); }
			set { SetValue(VerticalGridPenProperty, value); }
		}
		#endregion VerticalGridPen
		#endregion VerticalGrid

		/// <summary>
		/// Validates suggested <see cref="Visibility"/> property value.
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		private static bool Visibility_Validate(object value)
		{
			GridVisibility x = (GridVisibility)value;
			return (x == GridVisibility.AllTicks || x == GridVisibility.LongTicks || x == GridVisibility.Hidden);
		}
		#endregion Dependency properties

		/// <summary>
		/// Executes <see cref="NavigationCommands.Zoom"/> command.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
		private void ZoomExecutedCommandHandler(object sender, ExecutedRoutedEventArgs e)
		{
			double zoom = 1.2;
			if (e.Parameter != null)
			{
				try
				{
					zoom = Convert.ToDouble(e.Parameter);
				}
				catch (Exception ex)
				{
					Trace.WriteLine("ZoomCommandHandler " + ex.Message);
					return;
				}
			}

			if (zoom == 0.0)
				return;
			if (zoom < 0.0)
				zoom = -1.0 / zoom;

			hGrid.BeginInit();
			hGrid.HorizontalScale.Scale *= zoom;
			hGrid.VerticalScale.Scale *= zoom;
			hGrid.EndInit();
			vGrid.BeginInit();
			vGrid.HorizontalScale.Scale *= zoom;
			vGrid.VerticalScale.Scale *= zoom;
			vGrid.EndInit();
		}

		/// <summary>
		/// Handles the Click event of the Exit control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void Exit_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
	}
}
